<?php

class Admin_NewsletterController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->_helper->layout->setLayout('admin');	
       Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
       $newsletter_model = new Newsletter();
       $newsletter_arr = $newsletter_model->listNewsletters();
       // print_r($newsletter_arr); EXIT;
       $this->view->newsletter_arr = $newsletter_arr;
     
    }
    
    public function addAction()
    {
        $this->_helper->layout->setLayout('admin');	
		Zend_Loader::loadFile('../application/admin/forms/Newsletter.php');
        $newsletter_form = new Form_Newsletter();
        $this->view->title = "Add new News Letter";
        
        $this->view->newsletter_form = $newsletter_form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($newsletter_form->isValid($formData)) {
                $newsletter_title = $newsletter_form->getValue('newsletter_title');
                $newsletter_content = $newsletter_form->getValue('newsletter_content');
                $template_id = $newsletter_form->getValue('template_id');
           Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
           $newsletter_model = new Newsletter();
           $newsletter_model->addNewsletter($template_id,$newsletter_title, $newsletter_content);
           $this->_helper->redirector('index');
            } else {
                $newsletter_form->populate($formData);
            }
        }
            
    }

    public function editAction()
    {
    	$this->_helper->layout->setLayout('admin');	
        Zend_Loader::loadFile('../application/admin/forms/Newsletter.php');
        $newsletter_form = new Form_Newsletter();
        $this->view->form = $newsletter_form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($newsletter_form->isValid($formData)) {
                $newsletter_id = (int)$newsletter_form->getValue('newsletter_id');
                $newsletter_title = $newsletter_form->getValue('newsletter_title');
                $newsletter_content = $newsletter_form->getValue('newsletter_content');
                $template_id = $newsletter_form->getValue('template_id');
                Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
                $newsletter_model = new Newsletter();
                $newsletter_model->updateNewsletter($newsletter_id, $newsletter_title, $newsletter_content,$template_id);
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $newsletter_id = $this->_getParam('newsletter_id', 0);
            if ($newsletter_id > 0) {
                Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
                $newsletter_model = new Newsletter();
                $newsletter_form->populate($newsletter_model->getNewsletter($newsletter_id));
            }
        }
        
    }

    /*public function deleteAction()
    {
      $this->_helper->layout->setLayout('admin');	
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
               $newsletter_id = $this->getRequest()->getPost('id');
                Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
                $newsletter_model = new Newsletter();
                $newsletter_model->deleteNewsletter($newsletter_id);
            }
            $this->_helper->redirector('index');
        } else {
            $newsletter_id = $this->_getParam('newsletter_id', 0);
            Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
            $newsletter_model = new Newsletter();
            $this->view->newsletter = $newsletter_model->getNewsletter($newsletter_id);
        }
    }*/
    
      public function deleteAction()
          {
               $this->_helper->layout->setLayout('admin');	
               $newsletter_id = $this->_getParam('newsletter_id', 0);
           //    echo $hotel_id;exit;
                Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
                $Newsletter_model = new Newsletter();
                $Newsletter_model->deleteNewsletter($newsletter_id);
                 $this->_helper->redirector('index');
      
       
           }


}







